package lagou.homework.lagouhomework.Unit4.code.q1;

import java.io.*;
import java.util.List;

public class ListSet implements Serializable {
    // 定义写文件方法
    public static void writeList(List<Student> listStudents) {
        ObjectOutputStream listStu = null;

        try {
            listStu = new ObjectOutputStream(new FileOutputStream("./src/lagou/homework/lagouhomework/Unit4/code/q1/StudentList"));
            listStu.writeObject(listStudents);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != listStu) {
                try {
                    listStu.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    //  定义读文件方法
    public static List<Student> readList() {
        ObjectInputStream listStu = null;
        List<Student> listStudents =null;

        try {
            listStu = new ObjectInputStream(new FileInputStream("./src/lagou/homework/lagouhomework/Unit4/code/q1/StudentList"));
            // 读取文件
            Object obj = listStu.readObject();
            // 获取集合对象
            listStudents = (List<Student>)obj;
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (null != listStu) {
                try {
                    listStu.close();

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        // 返回集合对象
        return listStudents;
    }
}
